package com.cash.loan.modular.cash.controller;

import com.cash.loan.core.util.DateUtil;
import com.cash.loan.modular.cash.result.PageResult;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.StrategyService;
import com.cash.loan.modular.cash.transfer.CreateStrategyDto;
import com.cash.loan.modular.cash.transfer.LoanStrategyListVo;
import com.cash.loan.modular.cash.transfer.UpdateStrategyDto;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018-07-20 8:58
 */
@Controller
@RequestMapping(value = "/cash/strategy")
public class StrategyController {

    private static Logger logger = LoggerFactory.getLogger(StrategyController.class);

    private static final String PREFIX = "/cash/strategy/";

    @Autowired
    private StrategyService strategyService;

    @RequestMapping(value = "/listPage")
    public String toListPage() {
        return PREFIX + "strategy_list.html";
    }

    @RequestMapping(value = "/createPage")
    public String toCreatePage() {
        return PREFIX + "strategy_create.html";
    }

    @RequestMapping(value = "/editPage")
    public String editPage() {
        return PREFIX + "strategy_edit.html";
    }

    @RequestMapping(value = "/detailPage")
    public String detailPage() {
        return PREFIX + "strategy_detail.html";
    }

    @ApiOperation("策略文章列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse getStrategyList(@RequestParam(value = "keyword",required = false) String keyword,
                                          @RequestParam(value = "startDate",required = false) String startDate,
                                          @RequestParam(value = "endDate",required = false) String endDate,
                                          @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSzie) {
        Date sDate = null;

        Date eDate = null;

        if(StringUtils.isNotBlank(startDate)) {
            sDate = DateUtil.parseDate(startDate);
        }

        if(StringUtils.isNoneBlank(endDate)) {
            eDate = DateUtil.parseDate(endDate);
        }

        PageResult<LoanStrategyListVo> pageResult = strategyService.getLoanStrategyList(keyword,sDate,eDate,currentPage,pageSzie);

        return ServerResponse.getSuccess("success",pageResult);
    }

    @ApiOperation("创建新的策略文章")
    @PostMapping(value = "/create")
    @ResponseBody
    public ServerResponse createStrategy(@RequestBody CreateStrategyDto strategyDto,
                                         BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }

        Boolean saveResult = strategyService.createStrategy(strategyDto);

        if(!saveResult) {
            return ServerResponse.getErrorMessage("保存失败");
        }

        return ServerResponse.getSuccess("保存成功");
    }

    @ApiOperation("更新策略文章")
    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse updateStrategy(@RequestBody UpdateStrategyDto strategyDto,
                                         BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }

        Boolean saveResult = strategyService.updateStrategy(strategyDto);

        if(!saveResult) {
            return ServerResponse.getErrorMessage("更新失败");
        }

        return ServerResponse.getSuccess("更新成功");
    }

    @ApiOperation("获取详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public ServerResponse getStrategyById(@RequestParam(value = "id") Long id) {
        Map<String,Object> result = strategyService.getStrategyContentById(id);
        return ServerResponse.getSuccess("success",result);
    }

    @ApiOperation("删除文章")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse deleteStrategyById(@RequestParam(value = "id") Long id) {
        Boolean result = strategyService.deleteById(id);
        if(!result) {
            return ServerResponse.getErrorMessage("删除失败");
        }
        return ServerResponse.getSuccess("删除成功");
    }

    @ApiOperation("得到所有的文章类型")
    @GetMapping(value = "/listCate")
    @ResponseBody
    public ServerResponse getAllArticleType() {
        List<Map<String,Object>> cateList = strategyService.getCateList();
        return ServerResponse.getSuccess("success",cateList);
    }

    /**
     *更新就数据
     * @return
     */
    @ApiOperation("更新旧数据")
    @PostMapping(value = "/updateOldData")
    @ResponseBody
    public ServerResponse updateOldData() {
        return strategyService.updateOldData();
    }
}
